{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox float-e-margins">
        <div class="ibox-content">          
            <div class="row">
                <!--搜索框开始-->
                <div class="col-sm-12">
                    <div class="layui-form layui-form-item">
                            <div class="layui-input-inline">
                                <input type="text" id="key" class="layui-input search" name="key" placeholder="输入关键字" />
                            </div>
                            <div class="layui-input-inline">
                                <button class="layui-btn" id="btn-search">立即搜索</button>
                                <button  class="layui-btn layui-btn-normal" id="empty">重置</button>
                            </div>
                        </div>
                </div>
                <!--搜索框结束-->
                <!--操作框开始-->
                <div class="col-sm-12">
                    <div class="input-group" >
                        <button type="button" class="btn btn-primary" onclick="wk.href('{:url('add_rule')}')" >
                            <i class="fa fa-plus"></i> 添加菜单
                        </button>
                        <button  class="btn btn-danger m-l-xs layuiBtn" data-type="getCheckData">
                            <i class="fa fa-trash-o"></i> 批量删除
                        </button>
                    </div>
                </div>
                <!--操作框结束-->
            </div>
            <div class="hr-line-dashed"></div>
            <div class="example-wrap">
                <div class="example">
                    <table class="layui-hide" id="test" lay-filter="testReload"></table>
                    <!--状态模板-->
                    <script type="text/html" id="staBar">
                        <input type="checkbox" value="{{d.id}}"  lay-skin="switch"  lay-text="启用|禁用" lay-filter="OnOff"{{ d.status == 1 ? 'checked' : '' }}  >
                    </script>
                    <!--操作模板-->
                    <script type="text/html" id="opeBar">
                        <a href="javascript:;" onclick="wk.href('{:url('add_rule')}?id={{d.id}}')" class="btn btn-info btn-xs m-r-xs" title="添加子菜单"><i class="fa fa-sitemap"></i></a>
                        <a href="javascript:;" onclick="wk.href('{:url('edit_rule')}?id={{d.id}}')" class="btn btn-primary btn-xs m-r-xs" title="编辑"><i class="fa fa-pencil"></i></a>
                        <a href="javascript:;" onclick="del_menu(this,{{d.id}})" id="del_{{d.id}}" class="btn btn-danger btn-xs m-r-xs" title="删除"><i class="fa fa-trash-o"></i></a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    //layui渲染数据表格
    layui.use(['table','util'], function () {
        var table = layui.table
            , form = layui.form
            , util = layui.util

        //固定块
        util.fixbar({
            css: {right: 30, bottom: 30}
            ,bgcolor: '#9F9F9F'
        });
        table.render({
            elem: '#test'
            , url: '{:url("Menu/index")}'
            ,response: {
                statusCode: 220 //成功的状态码，默认：0
            }
            , page: true
            , even: false //开启隔行背景
            , size: 'lg' //小尺寸的表格
            // ,width : 1000
            , cellMinWidth: 150
            , limits: []
            , limit: 1000
            , loading: true
            , id: 'testReload'
            , cols: [[
                {type: 'checkbox', fixed: 'left',}
                , {type:'numbers', width: 80, title: '序号'}
                , {field: 'title', width: '' ,title: '权限名称', align: 'left',edit: 'text',templet:'<div>{{d.placeholder}}{{d.title}}</div>'}
                , {field: 'name', width: '', title: '节点', align: 'center',edit: 'text'}
                , {field: 'create_time', width: '', title: '添加时间', align: 'center'}
                , {field: 'sort',fixed: 'right', width: 90, title: '排序', align: 'center',edit: 'text'}
                , {field: 'status',fixed: 'right', width: 100, title: '状态', align: 'center', templet: '#staBar',hide:'{:session("?security")? false : true}'}
                , {fixed: 'right', width: 140, title: '操作', align: 'center', toolbar: '#opeBar'}
            ]],
            done: function (res, curr, count) {
                $('th').children().prop('align','center');
            }
        });
        //监听状态开关操作
        form.on('switch(OnOff)', function (obj) {
            var num = '';
            obj.elem.checked == true? num = 1: num = 2;
            wk.status(this.value,num, '{:url("rule_state")}',obj);
        });
        //监听单元格编辑
        table.on('edit(testReload)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            if(field == 'sort'){
                var patrn = /^[0-9]*$/;
                if(!patrn.test(value)){
                    layer.msg('排序值只能为数字',{icon:2,time:1500,shade: 0.1},function(){
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                    return false;
                }
                wk.change(data.id,"{:url('ruleorder')}",field,value);
            }else{
                wk.change(data.id,"{:url('editField')}",field,value);
            }
        });

        //批量操作
        var $ = layui.$, active = {
            getCheckData: function(){
                //批量删除
                layer.confirm('选中菜单的子分类将同时删除,确认删除吗?', {icon: 7, title:'警告'}, function(index){
                    $.ajax({
                        url: "{:url('batchDelMenu')}",
                        type: "post",
                        dataType: "json",
                        data: 'ids='+getIds(),
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1},function(index){
                                    layer.close(index);
                                    if (res.data != "") {
                                        var num = res.data.split(',');
                                        $.each(num, function (index, item) {
                                            $('#del_' + item).parents("tr").remove();
                                        })
                                    }
                                });
                            } else if (res.code == 100) {
                                wk.error(res.msg);
                            }
                        }
                    });
                    layer.close(index);
                })
            }
        };
        $('.layuiBtn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('testReload')
                ,data = checkStatus.data;
            $.each(data,function(index,item){
                ids.push(item['id'])
            });
            return ids;
        }
    });
</script>
<script type="text/javascript">
    /**
     * [del_menu 删除菜单]
     */
    function del_menu(obj,id){
        // wk.confirm(id,'{:url("del_rule")}');
        layer.confirm('此菜单的子分类将同时删除,确认删除吗?', {icon: 7, title:'警告'}, function(index){
            //do something
            $.getJSON('{:url("del_menu")}', {'id' : id}, function(res){
                if(res.code == 200){
                    layer.msg(res.msg,{icon:1,time:1500,shade: 0.1},function(index){
                        layer.close(index);
                        var num = res.data.split(',');
                        $.each(num, function (index, item) {
                            $('#del_' + item).parents("tr").remove();
                        })
                    });
                }else if(res.code == 100){
                    wk.error(res.msg);
                }
            });

            layer.close(index);
        })
    }

    // 模糊搜索表格
    $('#btn-search').click(function () {
        var keyword = $('#key').val();
        var searchCount = 0;
        $('#test').next('.layui-table-view').find('.layui-table-body tbody tr td').each(function () {
            $(this).css('background-color', 'transparent');
            var text = $(this).text();
            if (keyword != '' && text.indexOf(keyword) >= 0) {
                $(this).css('background-color', 'rgba(250,230,160,0.5)');
                if (searchCount == 0) {
                    $('html,body').stop(true);
                    $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
                }
                searchCount++;
            }
        });
        if (keyword == '') {
            wk.error("请输入搜索内容");
        } else if (searchCount == 0) {
            wk.error("没有匹配结果");
        }
    });
</script>
</body>
</html>